home *** CD-ROM | disk | FTP | other *** search
- /*==============================================================================
- *
- * File: MPWRefExample.c
- *
- * Function: QuickDraw 3D Metafile Read/Write Sample Code
- * This code shows how to resolve internal and external references
- * which are read from a QuickDraw 3D metafile.
- *
- * Version: Metafile: Version 1.0 3DMF files
- * Package: Release #2 of this code
- *
- * Author(s): Rick Wong (RWW), Duet Development Corp.
- * John Kelly (JRK), Duet Development Corp.
- *
- * Copyright: (c) 1995 by Apple Computer, Inc., all rights reserved.
- *
- * Change History (most recent first):
- * F3M_RWW Added this header.
- *==============================================================================
- */
-
- #include <stdio.h> /* printf */
-
- #ifdef __MWERKS__
- #include <console.h> /* ccommand */
- #endif
-
- #include "MF3D.H" /* MF3D API */
- #include "MFERRORS.H" /* MF3D Error codes */
-
- int
- main(int argc, char *argv[])
- {
- MF3D_FilePtr metafilePtr; /* MF3D internal file pointer */
- MF3DVoidObjPtr object; /* object returned by Read */
- int objCount; /* count the objects as we read */
- MF3DBoolean nextObjIsRoot; /* next obj read will be root */
- MF3DErr result; /* our result code */
- MF3DErr status; /* temporaty result code */
-
- #ifdef __MWERKS__
- argc = ccommand(&argv);
- #endif
-
- if (argc != 2)
- { printf("# Usage: %s fileName\n", argv[0]);
- return -1;
- }
-
- result = kMF3DNoErr;
-
- if (result == kMF3DNoErr)
- { /* Open the metafile */
- result = MF3DOpenInputStdCFile(argv[1], &metafilePtr);
- if (result != kMF3DNoErr)
- printf("# Open Input failed. Error %ld.\n", (long)result);
- }
-
- objCount = 0;
- nextObjIsRoot = kMF3DBooleanFalse;
-
- while (result == kMF3DNoErr)
- { /* Read an object from the metafile */
- result = MF3DReadAnObject(metafilePtr, &object);
-
- ++objCount;
-
- if (result != kMF3DNoErr)
- { if (result != kMF3DNoMoreObjects)
- { printf("# Read failed for object #%d. Error %ld.\n", objCount,
- (long)result);
- }
- }
- else if (object->objectType == kMF3DObjUnknownType)
- { MF3DObjType objectType = ((MF3DUnknownObjPtr)object)->realObjectType;
- char *objectName = ((MF3DUnknownObjPtr)object)->realObjectName;
- if (objectType == kMF3DObjUnknownType)
- objectType = 0x20202020; /* Four spaces */
- if (objectName == NULL)
- objectName = ""; /* Empty string */
- printf("# Read succeeded for unknown object #%d (%.4s, \"%s\").\n",
- objCount, &objectType, objectName);
- }
- else
- { printf("# Read succeeded for object #%d (%.4s).\n", objCount,
- &object->objectType);
- }
-
- if (result == kMF3DNoErr && object->objectType == kMF3DObjReference)
- { /* If we have just read a Reference object, resolve it.
- * There are two types of references: local and external.
- * An external reference will be the root object of a container
- * and will have a subobject specifying the path
- * to an external file.
- */
- MF3DVoidObjPtr extStorageObj;
-
- extStorageObj = NULL;
- if (nextObjIsRoot == kMF3DBooleanTrue)
- { /* We are in a container which must mean we have an
- * external reference.
- */
- result = MF3DReadAnObject(metafilePtr, &extStorageObj);
-
- ++objCount;
- if (result != kMF3DNoErr)
- { printf("# Read failed for object #%d. Error %ld.\n",
- objCount, (long)result);
- }
- else
- { printf("# Read succeeded for object #%d (%.4s).\n",
- objCount, &extStorageObj->objectType);
- }
-
- /* Because this sample code is written on a Macintosh,
- * we expect a Macintosh-style path here.
- */
- if (result == kMF3DNoErr &&
- extStorageObj->objectType != kMF3DObjMacintoshPath)
- { printf("# Resolve external reference failed because "
- "external storage is not a Macintosh path.\n");
- result = -2;
- }
- }
-
- /* Resolve the reference. This causes metafilePtr to point
- * to the referenced object, so that the next call to
- * MF3DReadAnObject will read the object which is being
- * referenced. MF3DReadAnObject will then restore the correct
- * file position after the referenced object has been read.
- *
- * If the reference is local, then extStorageObj is NULL.
- * If the reference is external, then the file specified by
- * extStorageObj will be opened automatically (and closed
- * automatically after the reference has been resolved).
- */
- if (result == kMF3DNoErr)
- { result = MF3DResolveReference(metafilePtr,
- (MF3DReferenceObjPtr) object,
- (MF3DStorageObjPtr) extStorageObj);
- }
-
- /* Dispose the external storage object, if there is one.
- * The Reference object will be disposed later.
- */
- MF3DDisposeObject(extStorageObj);
- }
-
- /* If the object we just read is a BeginContainer object, the next
- * object read will be the root object of the container.
- */
- if (result == kMF3DNoErr && object->objectType == kMF3DObjContainer)
- nextObjIsRoot = kMF3DBooleanTrue;
- else
- nextObjIsRoot = kMF3DBooleanFalse;
-
- /* Dispose the object (a real program would copy data first) */
- if (result == kMF3DNoErr)
- { result = MF3DDisposeObject(object);
- if (result != kMF3DNoErr)
- { printf("# Dispose failed for object #%d (%.4s). Error %ld.\n",
- objCount, &object->objectType, (long)result);
- }
- }
- }
-
- /* No error occurred if we ran out of objects while reading */
- if (result == kMF3DNoMoreObjects)
- result = kMF3DNoErr;
-
- /* Close does nothing if metafilePtr is NULL */
- status = MF3DClose(metafilePtr);
- if (status != kMF3DNoErr)
- { printf("# Close input failed. Error %ld.\n", (long)status);
- if (result == kMF3DNoErr)
- result = status;
- }
-
- return result;
- }
-